import axios from 'axios'

// 创建axios实例
const api = axios.create({
  timeout: 30000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
api.interceptors.request.use(
  config => {
    // 可以在这里添加认证信息等
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    return Promise.reject(error)
  }
)

// 压力测试相关API
export const testApi = {
  // 开始压力测试
  startTest: async (config) => {
    try {
      const response = await api.post('/api/test/start', config)
      return response
    } catch (error) {
      console.error('开始压力测试失败:', error)
      throw error
    }
  },
  
  // 停止压力测试
  stopTest: async (testId) => {
    try {
      const response = await api.post(`/api/test/${testId}/stop`)
      return response
    } catch (error) {
      console.error('停止压力测试失败:', error)
      throw error
    }
  },
  
  // 获取测试历史
  getTestHistory: async () => {
    try {
      const response = await api.get('/api/test/history')
      return response
    } catch (error) {
      console.error('获取测试历史失败:', error)
      throw error
    }
  },
  
  // 获取测试详情
  getTestDetail: async (testId) => {
    try {
      const response = await api.get(`/api/test/${testId}`)
      return response
    } catch (error) {
      console.error('获取测试详情失败:', error)
      throw error
    }
  },
  
  // 获取测试实时数据
  getTestRealtimeData: async (testId) => {
    try {
      const response = await api.get(`/api/test/${testId}/realtime`)
      return response
    } catch (error) {
      console.error('获取实时数据失败:', error)
      throw error
    }
  }
}

// 网站分析相关API
export const analyzeApi = {
  // 分析网站API
  analyzeWebsite: async (url) => {
    try {
      const response = await api.post('/api/analyze', { url })
      return response
    } catch (error) {
      console.error('分析网站失败:', error)
      throw error
    }
  },
  
  // 获取网站API列表
  getApiList: async (url) => {
    try {
      const response = await api.get(`/api/analyze/${encodeURIComponent(url)}/apis`)
      return response
    } catch (error) {
      console.error('获取API列表失败:', error)
      throw error
    }
  }
} 